//
//  ShipinCell.m
//  网易新闻
//
//  Created by dllo on 15/10/5.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "ShipinCell.h"
#import "SDWebImage/UIButton+WebCache.h"
#import "Settings.h"
@interface ShipinCell()<UIWebViewDelegate>
@property(nonatomic, retain)UILabel *title;
@property(nonatomic, retain)UILabel *desc;
@property(nonatomic, retain)UILabel *details;
@property(nonatomic, retain)UIWebView *player;
@property(nonatomic, retain)UIButton *img;
@end
@implementation ShipinCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
        self.selectedBackgroundView = [[[UIView alloc] initWithFrame:self.frame] autorelease];
        self.selectedBackgroundView.backgroundColor = [[Settings shareSettings]color:GrayToBlack];
        [self sendSubviewToBack:self.selectedBackgroundView];
        self.background.backgroundColor = [[Settings shareSettings]color:WhiteToLightGray];
        
        self.title = [[UILabel alloc]init];
        self.title.textColor = [[Settings shareSettings]color:BlackToWhite];
        [self.background addSubview:self.title];
        [self.title release];
        
        self.desc = [[UILabel alloc]init];
        self.desc.textColor = [UIColor lightGrayColor];
        self.desc.font = [UIFont systemFontOfSize:14];
        [self.background addSubview:self.desc];
        [self.desc release];
        
        self.details = [[UILabel alloc]init];
        self.details.textColor = [UIColor lightGrayColor];
        self.details.font = [UIFont systemFontOfSize:14];
        [self.background addSubview:self.details];
        [self.details release];

        self.player = [[UIWebView alloc] init];
        self.player.backgroundColor = [UIColor lightGrayColor];
        [(UIScrollView *)[[self.player subviews] objectAtIndex:0] setBounces:NO];
        self.player.allowsInlineMediaPlayback = YES;
        self.player.mediaPlaybackRequiresUserAction = NO;
        self.player.delegate = self;
        [self.background addSubview:self.player];
        [self.player release];
        
        self.img = [UIButton buttonWithType:UIButtonTypeCustom];
        self.img.backgroundColor = [UIColor lightGrayColor];
        [self.img addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.background addSubview:self.img];
        
        [self notificationHandle];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.background.frame = CGRectMake(12, 12, 350, 310);
    self.title.frame = CGRectMake(10, 210, 330, 20);
    self.desc.frame = CGRectMake(10, 240, 330, 15);
    self.details.frame = CGRectMake(10, 265, 330, 45);
    self.player.frame = CGRectMake(0, 0, 350, 197);
    self.img.frame = CGRectMake(10, 10, 330, 175);
}

- (void)setMod:(ShipinModel *)mod{
    if (_mod != mod) {
        [_mod release];
        _mod = [mod retain];
    }
    self.height = 325;
    self.title.text = mod.title;
    self.desc.text = mod.desc;
    self.details.text = [NSString stringWithFormat:@"时长: %-2d:%-2d       播放次数: %d        评论: %d", (int)mod.length/60, (int)mod.length%60, (int)mod.playCount, (int)mod.replyCount];
    [self.img sd_setImageWithURL:mod.cover forState:UIControlStateNormal];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    self.player.backgroundColor = [UIColor blackColor];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"erorr ShipinCell webViewdidFailLoad");
}

- (void)click:(UIButton *)button{
    button.hidden = YES;
    //全局断点会崩溃
    NSString *htmlStr = [NSString stringWithFormat:@"\
                         <!DOCTYPE HTML>\
                         <html>\
                         <body bgcolor=\"#000000\">\
                         <video controls=\"controls\" width=\"330\" height=\"175\" autoplay=\"autoplay\" webkit-playsinline>\
                         <source src=\"%@\" type=\"video/mp4\" />\
                         该浏览器不支持\
                         </video> </body> </html>",self.mod.mp4_url];
    [self.player loadHTMLString:htmlStr baseURL:nil];
}

- (void)notificationHandle{
    [[NSNotificationCenter defaultCenter]addObserverForName:@"changeBackgroundColor" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
        self.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
        self.background.backgroundColor = [[Settings shareSettings]color:WhiteToLightGray];
        self.selectedBackgroundView.backgroundColor = [[Settings shareSettings]color:GrayToBlack];
        self.title.textColor = [[Settings shareSettings]color:BlackToWhite];
    }];
}

- (void)dealloc{
    [_player stopLoading];
    _player.delegate = nil;
    [_title release];
    [_desc release];
    [_details release];
    [_player release];
    [super dealloc];
}
@end

